package com.wondersgroup.insurance.dnuilearn.views.path;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.MainThread;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2017/5/24.
 */

public class DeleteView extends View {

    Paint mPaint;
    Path mPath =null;

    int degree = 0;

    int cX, cY;

    int delW = 300,delH = 200;
    int halfW = delW/2;
    int bottomW = 150;
    int halfBottomW = bottomW/2;
    int padding =40;


    Bitmap bttomBmp ;
    Bitmap topBmp;
    public DeleteView(Context context) {
        this(context, null);
    }

    public DeleteView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DeleteView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        handler.sendEmptyMessageDelayed(100,3000);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        cX = getWidth()/2;
        cY = getHeight()/2;

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPath.reset();

        if(degree == 0){
            mPath.moveTo(cX -halfW,cY);
            mPath.lineTo(cX+halfW,cY);

            mPath.moveTo(cX-20,cY);
            mPath.rLineTo(0,-40);
            mPath.rLineTo(40,0);
            mPath.rLineTo(0,40);
        }else {
            double radius =    Math.toRadians(degree);
           double cosValue  = Math.cos(radius);
            double sinValue = Math.sin(radius);
            mPath.moveTo((float) (cX+halfW - padding - (delW-padding)*cosValue), (float) (cY-(delW-padding)*sinValue));
            mPath.rLineTo((float)(delW*cosValue),(float)(delW*sinValue));
            mPath.rMoveTo(-(float)((halfW+20)*cosValue),-(float)((halfW+20)*sinValue));
            mPath.rLineTo((float)(40*sinValue),(float)(-40*cosValue));
            mPath.rLineTo((float)(40*cosValue),(float)(40*sinValue));
            mPath.rLineTo(-(float)(40*sinValue),(float)(40*cosValue));

        }



        mPath.moveTo(cX+halfW,cY);
        mPath.rMoveTo(-padding,0);
        mPath.rLineTo(halfBottomW +padding- halfW,delH);
        mPath.rLineTo(-bottomW,0);
        mPath.rLineTo(padding+halfBottomW- halfW,-delH);
        mPath.rMoveTo(halfW-padding - halfBottomW+20, delH*0.2f);
        mPath.rLineTo(0,delH*0.6f);
        mPath.moveTo(cX,cY+ delH*0.2f);
        mPath.rLineTo(0,delH*0.6f);
        mPath.moveTo(cX+halfBottomW-20,cY+ delH*0.2f);
        mPath.rLineTo(0,delH*0.6f);

        canvas.drawPath(mPath,mPaint);
    }

    void init(){
        mPaint= new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.RED);
        mPaint.setStrokeWidth(6);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPath = new Path();

        bttomBmp = Bitmap.createBitmap(delW,delH, Bitmap.Config.ARGB_8888);
        topBmp = Bitmap.createBitmap(delW,45, Bitmap.Config.ARGB_8888);


//        mPath.moveTo( padding,0);

//        mPath.lineTo(halfW,0);
//        mPath.rMoveTo(-padding,0);
//        mPath.rLineTo(halfBottomW +padding- halfW,delH);
//        mPath.rLineTo(-bottomW,0);
//        mPath.rLineTo(padding+halfBottomW- halfW,-delH);
    }
    public void startAnimation() {
        ValueAnimator valueAnimator = null;
        if (degree == 0) {
            valueAnimator = ValueAnimator.ofInt(0, 80);
        } else {
            valueAnimator = ValueAnimator.ofInt(80, 0);

        }

        valueAnimator.setDuration(5000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                degree = (int) animation.getAnimatedValue();


                invalidate();
            }


        });
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                handler.sendEmptyMessageDelayed(100, 2000);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        valueAnimator.start();
    }
        Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                startAnimation();
            }
        };
}
